package com.zxs.user.service.impl;

import com.zxs.user.mapper.AddressMapper;
import com.zxs.user.service.AddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Address;
import utils.R;

import java.util.List;

/**
 * @author zxs
 * *7 11:48
 */
@Service
public class AddressServiceImpl implements AddressService {


    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查看用户的地址信息
     *
     * @param id
     * @return
     */
    @Override
    public R listAdresses(Integer id) {
        //1 封装查询条件
        QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>();
        queryWrapper.eq("user_id",id);
        //2 查询数据库
        List<Address> addressList = addressMapper.selectList(queryWrapper);
        //3 处理结果集
        return R.ok("查询成功！", addressList);
    }

    @Override
    public R remove(Integer id) {
        int deleteById = addressMapper.deleteById(id);
        if (deleteById == 0){
            return  R.fail("删除失败，请重新输入！");
        }
        return R.ok("删除成功");
    }

    @Override
    public R save(Address address) {
        int insert = addressMapper.insert(address);
        if (insert == 0){
            return R.fail("地址添加失败！");
        }
        //添加成功
        return listAdresses(address.getUserId());
    }

}
